/*
 * Copyright 2004-2006 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.sysframe.valid;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.companyname.common.sysframe.define.OrgStatus;
import com.companyname.common.sysframe.manager.OrgManager;
import com.companyname.common.sysframe.model.Org;
import com.companyname.common.util.mvc.valid.UserValidator;
import com.companyname.common.util.mvc.valid.ValidatedResult;

/**
 * <p>Title: 机构</p>
 * <p>Description: 机构 Org </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: 公司名</p>
 * @ $Author: 作者名 $
 * @ $Date: 创建日期 $
 * @ $Revision: 1.0 $
 * @ created in 创建日期
 *
 */

public class AddOrgValidator extends UserValidator
{
        static Logger logger = Logger.getLogger(AddOrgValidator.class);

        /**
         * 新增机构
         *
         * @ param: HttpServletRequest request
         * @ param: ValidatedResult vr 校验结果存放器
         * @ param: String[] params 配置参数
         */

        public void validing(HttpServletRequest request,ValidatedResult vr,String[] params)
        {
                logger.debug("开始进行新增机构前校验");
                OrgManager orgManager = (OrgManager)this.getBean("orgManager");
                
                String code = request.getParameter("code");
                if(orgManager.getByCode(code) != null)
                {
                        vr.setErrMess("code","已被使用");
                }
                this.validString(request,vr,"code",1,50);
                
                String name = this.validString(request,vr,"name",1,50);
                
                Org superOrg = null;
                String superOrgId = request.getParameter("parentId");
                if(superOrgId != null)
                {
                        superOrg = (Org)this.validBaseObjectById(request,vr,orgManager,"parentId","请重新选择");
                }
                
                String country = this.validString(request,vr,"country",0,50);
                
                String province = this.validString(request,vr,"province",0,50);
                
                String city = this.validString(request,vr,"city",0,50);
                
                String street = this.validString(request,vr,"street",0,100);
                
                String office = this.validString(request,vr,"office",0,50);
                
                String linkMan = this.validString(request,vr,"linkMan",0,50);
                
                String phone = this.validString(request,vr,"phone",0,50);
                
                String fax = this.validString(request,vr,"fax",0,50);
                
                String site = this.validString(request,vr,"site",0,50);
                
                String email = this.validString(request,vr,"email",0,50);
                
                String remark = this.validString(request,vr,"remark",0,500);
                
                if(vr.isAllValidated())
                {
                        logger.debug("通过校验");
                        Org org = new Org();
                        org.setCode(code);
                        org.setName(name);
                        org.setSuperOrg(superOrg);
                        org.setCountry(country);
                        org.setProvince(province);
                        org.setCity(city);
                        org.setStreet(street);
                        org.setOffice(office);
                        org.setLinkMan(linkMan);
                        org.setPhone(phone);
                        org.setFax(fax);
                        org.setSite(site);
                        org.setEmail(email);
                        org.setRemark(remark);
                        org.setStatus(OrgStatus.VALID);
                        request.setAttribute(Org.class.getName(),org);
                }
        }
}
